package oneD.models;

import oneD.dynamics.DynamicStructure;
import oneD.fem.Constraint;
import oneD.fem.Force;
import oneD.fem.Node;
import oneD.fem.structure.Structure;
import oneD.fem.structure.Truss;
import oneD.fem.visualize.DynamicTrussVisualizer;
import oneD.fem.visualize.TrussVisualizer;
import inf.jlinalg.SolveFailedException;
import inf.v3d.view.Viewer;

public class TwobarKuhl extends ModelViewer {

	@Override
	public Truss createStructure() {
		Truss struct = new Truss();
		double H = 0.7, B1 = 1, B2 = 0.1;
		double E = 2e7, A = 1, rho = 1, alpha = 100, beta = 100;
		double L1 = Math.sqrt(B1 * B1 + H * H);
		// double L2 = Math.sqrt(B2 * B2 + H * H);
		double r = E * A * Math.pow(H, 3) / 3 / Math.sqrt(3) / Math.pow(L1, 3);
		double lambda = 1;//
		/*
		 * choose 1 to compute linear dynamic response choose -0.68 to compute
		 * the instability point of the structure;
		 */
		Constraint c1 = new Constraint(true, false, false);
		Constraint c2 = new Constraint(false, true, false);
		Constraint c3 = new Constraint(false, false, false);
		Force f = new Force(0, lambda * r, 0);
		// create nodes
		Node n1 = struct.addNode(0.0, 0.0, 0.0);
		Node n2 = struct.addNode(B1, H, 0.0);
		Node n3 = struct.addNode(-B2, H, 0.0);
		// apply BCs
		n2.setForce(f);
		n1.setConstraint(c1);
		n2.setConstraint(c2);
		n3.setConstraint(c3);
		// create elements
		struct.addElement(rho, E, A, 0, 1);
		struct.addElement(beta * rho, alpha * E, A, 0, 2);
		// return the new structure
		return struct;
	}

	@Override
	public void initializeVisualizer(Structure struct) {
		this.view_ = new Viewer();
		viz_ = new TrussVisualizer(struct, this.view_);
	}

	@Override
	public void initializeVisualizer(DynamicStructure dStruct) {
		this.view_ = new Viewer();
		viz_ = new DynamicTrussVisualizer(dStruct, this.view_);
	}

	@Override
	protected void performNonlinearStaticAnalysis(Structure struct) {
		/*
		 * nonlinear static deformation solution
		 */
		System.out
				.println("This functionality is not supported for this model");
	}

	@Override
	protected void performNonlinearInstabilityAnalysisUsingSecondOrderTheory(
			Structure struct) {
		/*
		 * nonlinear static deformation solution
		 */
		System.out
				.println("This functionality is not supported for this model");
	}

	@Override
	protected void performNonlinearInstabilityAnalysisUsingDirectComputation(
			Structure struct) {
		/*
		 * nonlinear static deformation solution
		 */
		System.out
				.println("This functionality is not supported for this model");
	}

	@Override
	protected void performLoadDisplacementAnalysisUsingArcLength(
			Structure struct) {
		/*
		 * nonlinear static deformation solution
		 */
		System.out
				.println("This functionality is not supported for this model");
	}

	public static void main(String[] args) throws SolveFailedException {
		TwobarKuhl model = new TwobarKuhl();
		model.processUserInput();
	}

}
